package com.zxf.basic.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.appcompat.widget.Toolbar
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.SizeUtils
import com.zxf.basic.R
import com.zxf.basic.expand.signClick
import kotlinx.android.synthetic.main.layout_titlebar.view.*

class TitleView : FrameLayout {

    var onBackClick: ((v: View) -> Unit)? = null
    var onRightClick: ((v: View) -> Unit)? = null

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)

    @SuppressLint("RestrictedApi")
    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr) {
        LayoutInflater.from(context).inflate(R.layout.layout_titlebar, this)
        fitsSystemWindows = true
        val typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.TitleView)
        val backDrwable = typedArray.getDrawable(R.styleable.TitleView_back_icon)
        if (backDrwable != null) {
            ivBack.setImageDrawable(backDrwable)
        }
        val titleSize = typedArray.getDimension(R.styleable.TitleView_title_size,
            SizeUtils.sp2px(18f).toFloat())
        tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize)
        val titleColor = typedArray.getColor(R.styleable.TitleView_title_color, Color.BLACK)
        tvTitle.setTextColor(titleColor)
        val rightDrawable = typedArray.getDrawable(R.styleable.TitleView_right_icon)
        ivRight.setImageDrawable(rightDrawable)
        typedArray.recycle()

        ivBack.signClick {
            onBackClick?.invoke(it)
        }

        ivRight.signClick {
            onRightClick?.invoke(it)
        }

    }

}